package com.lingjtx.oss.common;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.lingjtx.common.core.constant.Const;
import com.lingjtx.common.core.constant.OSSFolderName;
import com.lingjtx.common.core.util.DateTimeUtil;
import com.lingjtx.common.core.util.StringUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

public class AliyunOSSUtil {

    private static final OSS CLIENT;

    static {
        CLIENT = new OSSClientBuilder().build(Const.ALIYUN_OSS_ENDPOINT, Const.ALIYUN_ACCESSKEY, Const.ALIYUN_ACCESS_SECRET);
    }

    public static boolean uploadFile(String bucketName, String objectName, InputStream ins) {
        try {
            CLIENT.putObject(bucketName, objectName, ins);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String getFileUri(Integer folderType, String originalFileName) {
        return OSSFolderName.codeOf(folderType).getFolder()
                + StringUtil.getUuid()
                + originalFileName.substring(originalFileName.lastIndexOf("."));
    }
}
